function [mineral_result, lipid_result, collagen_result, Hb_result, HHb_result, BOS_result] = PAMolecularImagingUnmixing(pa_image_list, us_image, image_width, image_height, wavelengths)
% 根据光声图像解混得到不同化合物图像的接口

% 输入参数：
% pa_image_list   :  不同波长光声图像的列表
% us_image        ： 超声图像，光声图像，超声图像的分辨率需一致
% wavelengths: 输入激光波长,为1*N的数组，数组内的数据数值在690：10：950范围内

% 输出参数：
%mineral_result   ： 矿物质含量分布图
%lipid_result     ： 脂质含量分布图
%collagen_result  ： 胶原蛋白含量分布图
%Hb_result        ： 血红蛋白含量分布图
%HHb_result       ： 脱氧血红蛋白含量分布图
%BOS_result       ： 血氧饱和度含量分布图
%%


disp("datatype:");
disp(class(us_image));
disp("dimensions of us_image:");
disp(ndims(us_image));
us_image_reshape = reshape(us_image, image_height, image_width);
disp("dimensions of us_image after reshape:");
disp(ndims(us_image_reshape));
disp("size of us_image after reshape:");
disp(size(us_image_reshape));
%figure;
%imshow(us_image_reshape);
disp("list of wavelength:");
disp(wavelengths);
disp("dimensions of wavelengths:");
disp(ndims(wavelengths));
disp("wavelengths datatype:");
disp(class(wavelengths));
disp("size of wavelengths");
disp(size(wavelengths));

numElements = numel(pa_image_list);
% Displaying the result
disp(['Number of elements in the pa image list: ' num2str(numElements)]);



size_lambda = numElements;  % 计算激光波长数量
for i = 1 : numElements
    img1 = reshape(pa_image_list{i}, image_height, image_width);
    %img1 = rgb2gray(img); 
    img1(find(img1<10))=0;
    img1=double(img1);
    y(:,:,i) = img1;
%     figure;
%     imshow(img1);
end

% pause;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

us_im=us_image_reshape;
size_x = image_height; 
size_y = image_width; 

% 690nm-950nm 间隔10nm
WL = 690:10:950;
%[aa,index_wavelength] = find(WL==wavelengths);
[aa, index_wavelength] = ismember(wavelengths, WL);
disp("index_wavelength:");
disp(index_wavelength);

A1=[0.032539,0.031755,0.030539,0.02872,0.029205,0.027852,0.028086,0.027389,0.024496,0.023491,0.022654,0.023822,0.022909,0.02234,0.02233,0.021726,0.021484,0.019704,0.02235,0.022909,0.026193,0.02945,0.031933,0.032929,0.0317554,0.0302839,0.0306239];%矿物质
A2=[0.001313,0.001717,0.002522,0.003633,0.005613,0.006951,0.007461,0.007701,0.007308,0.00665,0.005967,0.005807,0.005977,0.007096,0.008763,0.010522,0.012319,0.014065,0.01579,0.016902,0.018194,0.019695,0.022931,0.03221,0.043727,0.06484,0.084582];%水
A3=[0.028935,0.025135,0.021881,0.018678,0.016214,0.015572,0.020307,0.023471,0.018315,0.015373,0.012546,0.010922,0.010199,0.009878,0.009924,0.009691,0.009817,0.010029,0.010131,0.009867,0.01008,0.011045,0.011126,0.011176,0.0111637,0.0101079,0.0088241];%脱氧血红蛋白
A4=[0.009873,0.010783,0.011466,0.01282,0.014454,0.016298,0.019161,0.021665,0.024089,0.025974,0.027852,0.030031,0.032563,0.033861,0.036106,0.037755,0.039479,0.040258,0.041745,0.042927,0.043774,0.044265,0.045138,0.045265,0.0451383,0.0445128,0.0445128];%含氧血红蛋白
A5=[0.001241,0.001448,0.001641,0.00152,0.002985,0.003775,0.006283,0.008914,0.004802,0.002205,0.001564,0.002115,0.002652,0.003935,0.004484,0.004015,0.003476,0.003906,0.005944,0.011133,0.022365,0.030849,0.049793,0.075768,0.0939388,0.0668161,0.0270368];%脂质
A6=[0.0412,0.0351,0.0302,0.0259,0.0235,0.0211,0.0181,0.0152,0.0130 ,0.0107,0.0098,0.0077,0.0070 ,0.0047,0.0039,0.0034,0.0026,0.0021,0.0030 ,0.0060 ,0.0117,0.0213,0.0297,0.0305,0.0282,0.0213,0.0143];%胶原

absorption(1,:)=A1(index_wavelength);
absorption(2,:)=A2(index_wavelength);
absorption(3,:)=A3(index_wavelength);
absorption(4,:)=A4(index_wavelength);
absorption(5,:)=A5(index_wavelength);
absorption(6,:)=A6(index_wavelength);

lambda =0.1e1;
copt = C_ML_unmixing(y, absorption',lambda);

Comp1 = copt(:,:,1); % 矿物质
Comp2 = copt(:,:,2); % 脂质
Comp3 = copt(:,:,3); % 胶原
Comp4 = copt(:,:,4); % 脱氧血红蛋白
Comp5 = copt(:,:,5); % 氧合血红蛋白
Comp6 = copt(:,:,6); % 水
Comp4(Comp4<0) = 0;
Comp5(Comp5<0) = 0;
Comp7 = Comp5./(Comp4+Comp5+1e-9); % 血氧饱和度

Comp1 = imadjust(uint8(255.*(Comp1 - min(Comp1(:) ))/(max(Comp1(:))-min(Comp1(:) ))));
Comp2 = imadjust(uint8(255.*(Comp2 - min(Comp2(:) ))/(max(Comp2(:))-min(Comp2(:) ))));
Comp3 = imadjust(uint8(255.*(Comp3 - min(Comp3(:) ))/(max(Comp3(:))-min(Comp3(:) ))));
Comp4 = imadjust(uint8(255.*(Comp4 - min(Comp4(:) ))/(max(Comp4(:))-min(Comp4(:) ))));
Comp5 = imadjust(uint8(255.*(Comp5 - min(Comp5(:) ))/(max(Comp5(:))-min(Comp5(:) ))));
Comp6 = imadjust(uint8(255.*(Comp6 - min(Comp6(:) ))/(max(Comp6(:))-min(Comp6(:) ))));
Comp7 = imadjust(uint8(255.*(Comp7 - min(Comp7(:) ))/(max(Comp7(:))-min(Comp7(:) ))));

Comp1(find(Comp1==Comp1(10,10)))=0;
Comp2(find(Comp2==Comp2(10,10)))=0;
Comp3(find(Comp3==Comp3(10,10)))=0;
Comp4(find(Comp4==Comp4(10,10)))=0;
Comp5(find(Comp5==Comp5(10,10)))=0;
Comp6(find(Comp6==Comp6(10,10)))=0;

rgbComp1 = cat(3, Comp1*0, Comp1*0, Comp1*1);%(0,0,1)蓝色  矿物质
rgbComp2 = cat(3, Comp2*1,Comp2*1, Comp2*0);%（1,1,0）黄色 脂肪
rgbComp3 = cat(3, Comp3*0,Comp3*1, Comp3*1);%（0,1,1）浅蓝 胶原
rgbComp4 = cat(3, Comp4*1, Comp4*0, Comp4*0);%（1,0,0）红色 脱氧血红蛋白
rgbComp5 = cat(3, Comp5*1, Comp5*0, Comp5*1);%（1,0,1）品红 含氧血红蛋白
rgbComp6 = cat(3, Comp6*1, Comp6*0, Comp6*0.5);%（0,1,0） 绿色 水
rgbComp7 = cat(3, Comp7*1,Comp7*0, Comp7*0.5);%（0,1,1）血氧饱和度
 
overlayed_image1=rgbComp1+us_im; % 矿物质
overlayed_image2=rgbComp2+us_im; % 脂肪
overlayed_image3=rgbComp3+us_im; %胶原
overlayed_image4=rgbComp4+us_im; %脱氧血红蛋白
overlayed_image5=rgbComp5+us_im; %含氧血红蛋白
% overlayed_image6=rgbComp6+us_im; %水     （水的分布图不用显示）
overlayed_image7=rgbComp7+us_im; %血氧饱和度
 

%% mineral_result, 一个图像（mat）需包括如下元素（image, dimension, pixel_spacing, channels, datatype, Colormap, XTick, XTickLabel, YTick, YTickLabel）用于前端显示
mineral_result.image = rot90(overlayed_image7,3);
mineral_result.dimension = size(mineral_result.image);
disp("mineral_result.dimension：");
disp(mineral_result.dimension);
%imwrite(mineral_result.image, "mineral_result.bmp");

%% 如下为mock结果
%fid = fopen("../const_file/mineral_result.bmp", "r", "b");
%file_content = fread(fid);
%mineral_result.image = cast(file_content, "uint8");
%fclose(fid);

%%  lipid_result, 一个图像（mat）需包括如下元素用于前端显示
lipid_result.image = rot90(overlayed_image4,3);
lipid_result.dimension = size(lipid_result.image);
%% 如下为mock结果
%fid = fopen("../const_file/lipid_result.bmp", "r", "b");
%file_content = fread(fid);
%lipid_result.image = cast(file_content, "uint8");
%fclose(fid);

%%  collagen_result, 一个图像（mat）需包括如下元素用于前端显示
collagen_result.image = rot90(overlayed_image5,3);
collagen_result.dimension = size(collagen_result.image);
%% 如下为mock结果
%fid = fopen("../const_file/collagen_result.bmp", "r", "b");
%file_content = fread(fid);
%collagen_result.image = cast(file_content, "uint8");
%fclose(fid);

%%  Hb_result, 一个图像（mat）需包括如下元素用于前端显示
Hb_result.image = rot90(overlayed_image2,3);
Hb_result.dimension = size(Hb_result.image);
%% 如下为mock结果
%fid = fopen("../const_file/Hb_result.bmp", "r", "b");
%file_content = fread(fid);
%Hb_result.image = cast(file_content, "uint8");
%fclose(fid);

%%  HHb_result, 一个图像（mat）需包括如下元素用于前端显示
HHb_result.image = rot90(overlayed_image3,3);
HHb_result.dimension = size(HHb_result.image);
%% 如下为mock结果
%fid = fopen("../const_file/HHb_result.bmp", "r", "b");
%file_content = fread(fid);
%HHb_result.image = cast(file_content, "uint8");
%fclose(fid);

%%  BOS_result, 一个图像（mat）需包括如下元素用于前端显示
BOS_result.image = rot90(overlayed_image1,3);
BOS_result.dimension = size(BOS_result.image);
%% 如下为mock结果
%fid = fopen("../const_file/BOS_result.bmp", "r", "b");
%file_content = fread(fid);
%BOS_result.image = cast(file_content, "uint8");
%fclose(fid);

%Boyi——20231125         
% 交换      
% 1<------>6(7)
% 2<------>4
% 3<------>5


% APP
% 1  2  3
% 4  5  6
end